package org.example.douban.dao;

import org.example.framework.domain.douban.BookUrls;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wwy
 * @date 2020-11-07 11:38
 */
public interface BookUrlsRepository extends JpaRepository<BookUrls,String> {
    /**
     * 根据标签查询
     * @param tag
     * @return
     */
    List<BookUrls> findByTag(String tag);

    /**
     * 根据url查询,如果出现重复，获取第一条
     * @param bookUrl
     * @param tag
     * @return
     */
    BookUrls findFirstByBookUrlAndTag(String bookUrl,String tag);

    /**
     * 爬取之后，避免重复爬取，更新爬取状态
     * @param isCrawled
     * @param bookUrl
     */
    @Transactional
    @Modifying
    @Query("update BookUrls m set m.isCrawled=:isCrawled where  m.bookUrl=:bookUrl and m.tag=:tag")
    void updateIsCrawledBybookurl(@Param("isCrawled")int isCrawled, @Param("bookUrl") String bookUrl,@Param("tag") String tag);
}
